import request from '@/utils/request'
import { makeFormData } from '@/utils/util'

const api = {
  attachmentList: 'crm/web/attachment/page', // 附件列表
  leadsHistory: 'crm/web/leadsHistory/find_by_leadsId',
  leadsHistoryByClient: 'crm/web/leadsHistory/find_by_clientelesId',
  leadsHistoryByContract: 'crm/web/contract_history/find_by_contract',
  staffList: 'web/staff/area_staff',
  attachmentAdd: 'crm/web/attachment/add',
  transferStaffList: 'web/staff/area_staff_by_permission'
}

export function attachmentList (data = {}) {
  return request({
    url: api.attachmentList,
    method: 'post',
    data: makeFormData(data)
  })
}

export function leadsHistory (data = {}, type = 'leadsHistory') {
  return request({
    url: api[type],
    method: 'post',
    data: makeFormData(data)
  })
}

export function getStaffList (data = {}) {
  return request({
    url: api.staffList,
    method: 'post',
    data: makeFormData(data)
  })
}

export function getTransferStaffList (data = {}) {
  return request({
    url: api.transferStaffList,
    method: 'get',
    params: data
  })
}

export function attachmentAdd (data = {}) {
  return request({
    url: api.attachmentAdd,
    method: 'post',
    data: makeFormData(data)
  })
}

const productApi = {
  list: 'crm/web/product/page',
  delete: 'crm/web/product/delete',
  create: 'crm/web/product/add',
  update: 'crm/web/product/update'
}

export function productList (params = {}) {
  return request({
    url: productApi.list,
    method: 'get',
    params
  })
}

export function productDelete (data = {}) {
  return request({
    url: productApi.delete,
    method: 'delete',
    params: data
  })
}

export function productSubmit (data = {}, type) {
  return request({
    url: productApi[type ? 'update' : 'create'],
    method: 'post',
    data: makeFormData(data)
  })
}

const clueApi = {
  channel: '/crm/web/channel/all',
  info: 'crm/web/leads/detail_with_clientele',
  add: 'crm/web/leads/add',
  delete: 'crm/web/leads/delete',
  transfer: 'crm/web/leads/update_leads_staff',
  follow: 'crm/web/leadsHistory/add'
}

export function getChannels (params = {}) {
  return request({
    url: clueApi.channel,
    method: 'post',
    params
  })
}

export function getClueInfo (params = {}) {
  return request({
    url: clueApi.info,
    method: 'post',
    params
  })
}

export function clueSubmit (data = {}) {
  return request({
    url: clueApi.add,
    method: 'post',
    data: makeFormData(data)
  })
}

export function clueDelete (data = {}) {
  return request({
    url: clueApi.delete,
    method: 'post',
    data: makeFormData(data)
  })
}

export function clueTransfer (data = {}) {
  return request({
    url: clueApi.transfer,
    method: 'post',
    data: makeFormData(data)
  })
}

export function clueFollow (data = {}) {
  return request({
    url: clueApi.follow,
    method: 'post',
    data: makeFormData(data)
  })
}

const clientApi = {
  info: 'crm/web/clientele/detail',
  checkContractCode: 'crm/web/contract/check_contract_code',
  transfer: 'crm/web/clientele/update_clientele_staff',
  add: 'crm/web/clientele/change_clientele',
  update: 'crm/web/clientele/update',
  delete: 'crm/web/clientele/delete'
}

export function clientInfo (data = {}) {
  return request({
    url: clientApi.info,
    method: 'post',
    data: makeFormData(data)
  })
}

export function clientUpdate (data = {}) {
  return request({
    url: clientApi.update,
    method: 'post',
    data: makeFormData(data)
  })
}

export function toClientSubmit (params = {}, data = {}) {
  return request({
    url: clientApi.add,
    method: 'post',
    params,
    data: makeFormData(data)
  })
}

export function clientTransfer (data = {}) {
  return request({
    url: clientApi.transfer,
    method: 'post',
    data: makeFormData(data)
  })
}

export function clientDelete (data = {}) {
  return request({
    url: clientApi.delete,
    method: 'post',
    data: makeFormData(data)
  })
}

export function checkContractCode (params = {}) {
  return request({
    url: clientApi.checkContractCode,
    method: 'post',
    params
  })
}

const contractApi = {
  list: 'crm/web/contract/page',
  listByBaby: 'crm/web/contract/find_contract_sysChildName',
  add: 'crm/web/contract/add',
  info: 'crm/web/contract/detail',
  transfer: 'crm/web/contract/update_contract_staff',
  freeze: 'crm/web/contract/freeze_contract',
  unfreeze: 'crm/web/contract/unfreeze_contract',
  stop: 'crm/web/contract/break_down',
  addBack: 'crm/web/contract_history/add'
}

export function getContracts (data = {}) {
  return request({
    url: contractApi.list,
    method: 'post',
    data: makeFormData(data)
  })
}

export function getContractsByBaby (params = {}) {
  return request({
    url: contractApi.listByBaby,
    method: 'post',
    params
  })
}

export function getContractInfo (data = {}) {
  return request({
    url: contractApi.info,
    method: 'post',
    data: makeFormData(data)
  })
}

export function contractTranfer (data = {}) {
  return request({
    url: contractApi.transfer,
    method: 'post',
    data: makeFormData(data)
  })
}

export function contractAdd (data = {}) {
  return request({
    url: contractApi.add,
    method: 'post',
    data: makeFormData(data)
  })
}

export function contractFreeze (params = {}) {
  return request({
    url: contractApi.freeze,
    method: 'post',
    params
  })
}

export function contractUnfreeze (params = {}) {
  return request({
    url: contractApi.unfreeze,
    method: 'post',
    params
  })
}

export function contractStop (data = {}) {
  return request({
    url: contractApi.stop,
    method: 'post',
    data: makeFormData(data)
  })
}

export function contractBackMoney (data = {}) {
  return request({
    url: contractApi.addBack,
    method: 'post',
    data: makeFormData(data)
  })
}
